//弹出提示
$(function(){
    if (!_messages) {
        return;
    }
    var html = '';
    for (var i in _messages) {
        var data = _messages[i];
        var msg = data[0];
        var code = data[1];
        var type = data[2];
        var t = 'info';
        if (type == 1) {
            t = 'error';
        } else if (type == 2) {
            t = 'warning';
        } else if (type == 3) {
            t = 'info';
        } else if (type == 4) {
            t = 'success';
        }
        html += '<div class="alert alert-' + t + '">';
        html += '<button type="button" class="close" data-dismiss="alert">×</button>';
        html += msg;
        html += '</div>';
    }
    $('#message-box').html(html);
});

// 绑定添加条件
$(function() {
    var defaultObj = $('.js-condition-tr').eq(0).clone();
    defaultObj.find('td').eq(0).html('');
    var a = '<a class="f12 jy_red pole jyu1 fl js-condition-del" href="javascript:void(0)">删除</a>';
    defaultObj.find('td').eq(-1).html(a);
    var tableObj = $('#js-condition-table');

    $('#js-add-condition').bind('click', function() {
        var obj = defaultObj.clone();
        tableObj.find('tbody').append(obj);
        obj.find('.js-c1').category({
            c2: obj.find('.js-c2'),
            c3: obj.find('.js-c3'),
            params:params
        });
        var ln = tableObj.find('tr').length;
        if (ln >= 5) {
            $('#js-add-condition').parent().parent().hide();
        }
    });
    tableObj.on('click', '.js-condition-del', function() {
        $(this).parents('tr').remove();
        $('#js-add-condition').parent().parent().show();
    });
});

$.fn.category = function(config) {
    var fn = $(this);
    var data = config.data || {};
    var pid = config.pid || 0;
    var currentId = config.currentId || 0;
    var selectAll = config.selectAll ? true : false;
    var root = '';
    if (config.showRoot) {
        root = '<option value="0">根分类</option>';
    }
    var str = root + getOption(data);
    $(this).html(str);
    function getOption(json, span, isDis) {
        var str = '';
        if (!span) {
            span = '';
        }
        span += '----';
        if (!json) {
            return str;
        }
        for (var i in json) {
            var disabled = '';
            var d = json[i];
            var selected = d.CategoryId == pid ? ' selected ' : '';

            if (currentId == d.CategoryId || isDis) {
                disabled = ' disabled';
            }

            if (selectAll) {
                var defaultId = fn.attr('defaultId');
                if (defaultId && defaultId == d.CategoryId) {
                    str += '<option class="" value="' + d.CategoryId + '"  selected>' + span + d.CategoryName +'</option>';
                } else {
                    str += '<option class="" value="' + d.CategoryId + '">' + span + d.CategoryName +'</option>';
                }
            } else {
                str += '<option class="'+disabled+'" value="' + d.CategoryId + '" ' + selected + disabled +'>' + span + d.CategoryName +'</option>';
            }

            if (d.List) {
                if (currentId == d.CategoryId || isDis) {
                    str += getOption(d.List, span, true);
                } else {
                    str += getOption(d.List, span, false);
                }
            }
        }
        return str;
    }
};

//根据浏览器窗口大小设置高度
function set_publicHeight(obj){
    var winHeight = $(window).height();
    var pubHeight = $(obj).height();
    var footerHeight = $('#footer')[0].offsetHeight;
    var setHeight = winHeight-$('.header:eq(0)').height()-footerHeight;
    //var s = $('.bg:eq(0)').css('padding-top');
    //var bgHeight = s.substring(0, s.length-2);
    //bgHeight = setHeight -bgHeight*1;
    //alert(bgHeight+"____"+setHeight);
    if(pubHeight < setHeight){
        $(obj).css('height',setHeight);
    }
    //alert(winHeight+"____"+pubHeight+"____"+setHeight+"____"+$('#footer').height());
}



